import React, {Component} from 'react'
import BScroll from 'better-scroll'
import {findDOMNode} from "react-dom"
import PropTypes from "prop-types"
import './srcoll.styl'
class Scroll extends Component {
    constructor(props) {
        super(props)
        this.state = {}
    }
    componentDidMount () {
        let scroll_wrapper = findDOMNode(this.refs.scroll_wrapper)
        if (!this.bScroll) {
            this.bScroll = new BScroll(scroll_wrapper, {
                scrollX: this.props.direction === "horizontal",
                scrollY: this.props.direction === "vertical",
                //实时派发scroll事件
                probeType: 3,
                click: this.props.click
            })
            console.log(this.bScroll)
            if (this.props.onScroll) {
                this.bScroll.on("scroll", (scroll) => {
                    this.props.onScroll(scroll);
                });
            }
        }
    }
    render() {
        return (
            <div className='scroll_wrapper' ref='scroll_wrapper'>
                {this.props.children}
            </div>
        )
    }
}

Scroll.defaultProps = {
    direction: "vertical",
    click: true,
    refresh: false,
    onScroll: null
};

Scroll.propTypes = {
    direction: PropTypes.oneOf(['vertical', 'horizontal']),
    //是否启用点击
    click: PropTypes.bool,
    //是否刷新
    refresh: PropTypes.bool,
    onScroll: PropTypes.func
};

export default Scroll